package com.tank.model;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

import com.tank.common.Dir;
import com.tank.config.ResourceMgr;
import com.tank.config.TankConfig;
import com.tank.util.ImageUtils;

/**
 * TODO
 *
 * @author chenwb
 * @since 2024/1/21 14:34
 */
public class Bullet implements Liver {

    private int x, y;
    private final Dir dir;
    private final int speed;
    private boolean isAlive = true;
    /**
     * 图片
     */
    private final BufferedImage bufferedImage;
    private Rectangle rectangle;

    public Bullet(int x, int y, Dir dir) {
        this(x, y, dir, ResourceMgr.getBulletImage(dir));
    }

    public Bullet(int x, int y, Dir dir, BufferedImage bufferedImage) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.speed = TankConfig.DEFAULT_BULLET_SPEED;
        this.bufferedImage = bufferedImage;
    }

    @Override
    public void die() {
        this.isAlive = false;
    }

    public boolean isAlive() {
        if (!this.isAlive) {
            return true;
        }
        if (x < 0 || x > TankConfig.GAME_WIDTH || y < 0 || y > TankConfig.GAME_HEIGHT) {
            this.isAlive = false;
            return true;
        }
        return false;
    }

    @Override
    public Rectangle getRectangle() {
        if (rectangle != null) {
            return rectangle;
        }
        return new Rectangle(x, y, this.bufferedImage.getWidth(), this.bufferedImage.getHeight());
    }

    public void paint(Graphics g) {
        g.setColor(Color.BLACK);
        // g.drawRect(x, y, this.bufferedImage.getWidth(), this.bufferedImage.getHeight());
        // 根据方向画出子弹
        g.drawImage(ImageUtils.getImageByDir(bufferedImage, dir), getDrawX(), getDrawY(), null);
        move();
    }

    private int getDrawX() {
        switch (dir) {
            case UP:
                return x - this.bufferedImage.getWidth() / 2;
            case DOWN:
                return x - this.bufferedImage.getWidth() / 2;
            case LEFT:
                return x - this.bufferedImage.getWidth();
            case RIGHT:
                return x;
            default:
                break;

        }
        return x;
    }

    private int getDrawY() {
        switch (dir) {
            case UP:
                return y - this.bufferedImage.getHeight();
            case DOWN:
                return y;
            case LEFT:
                return y - this.bufferedImage.getWidth() / 2;
            case RIGHT:
                return y - this.bufferedImage.getWidth() / 2;
            default:
                break;
        }
        return y;
    }

    public void move() {
        switch (dir) {
            case UP:
                y -= this.speed;
                break;
            case DOWN:
                y += this.speed;
                break;
            case LEFT:
                x -= this.speed;
                break;
            case RIGHT:
                x += this.speed;
                break;
            default:
                break;
        }
    }
}
